package com.subat.demo.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subat.demo.common.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.subat.demo.service.IMajorService;
import com.subat.demo.entity.Major;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author subat
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/major")
public class MajorController {

    @Resource
    private IMajorService majorService;

    private final String now = DateUtil.now();

//    新增或者更新信息
    @PostMapping
    public Result save(@RequestBody Major major) {
        majorService.saveOrUpdate(major);
        return Result.success();
    }

//    删除信息
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        majorService.removeById(id);
        return Result.success();
    }

//    批量删除信息
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        majorService.removeByIds(ids);
        return Result.success();
    }

//    查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(majorService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(majorService.getById(id));
    }

    //    分页查询和模糊查询（Mybatis-plus版）
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(majorService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
